package com.home.model.service.impl;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.home.model.dao.AccountDao;
import com.home.model.dao.CityDao;
import com.home.model.dao.DistrictDao;
import com.home.model.dao.UserDao;
import com.home.model.dto.AccountDto;
import com.home.model.dto.CityDto;
import com.home.model.dto.DistrictDto;
import com.home.model.dto.UserDto;
import com.home.model.entity.Account;
import com.home.model.entity.User;
import com.home.model.service.UserService;

@Stateless
public class UserServiceImpl implements UserService {

	@EJB
	private UserDao userDao;
	@EJB
	private AccountDao accountDao;
	@EJB
	private CityDao cityDao;
	@EJB
	private DistrictDao districtDao;
	
	/**
	 * find user by account id
	 * @param accountDto
	 * @return
	 */
	public UserDto findUserByAccount(AccountDto accountDto) {
		UserDto userDto = null;
		User user = userDao.find("accountId", accountDto.getId());
		if (user != null) {
			userDto = new UserDto(user);
			CityDto cityDto = new CityDto(cityDao.find(user.getCityId()));
			DistrictDto districtDto = new DistrictDto(districtDao.find(user.getDistrict()));
			userDto.setCityDto(cityDto);
			userDto.setDistrictDto(districtDto);
		}
		return userDto;
	}
	
	/**
	 * find user by user name
	 * @param userName
	 * @return
	 */
	public UserDto findUserByUserName(String userName) {
		UserDto userDto = null;
		
		Account account = accountDao.find("userName", userName);
		userDto = findUserByAccount(new AccountDto(account));
		
		return userDto;
	}


}
